package com.atguigu.gmall.canal.config;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.model.cart.CartInfo;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;

/**
 * @author chen.qian
 * @date 2018/3/19
 */
@CanalEventListener
public class MyEventListener {

   /* @InsertListenPoint
    public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }

    @UpdateListenPoint
    public void onEvent1(CanalEntry.RowData rowData) {
        System.err.println("UpdateListenPoint");
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }

    @DeleteListenPoint
    public void onEvent3(CanalEntry.EventType eventType) {
        System.err.println("DeleteListenPoint");
    }*/

    @Autowired
    private RedisTemplate redisTemplate;

    @ListenPoint(destination = "example",
            schema = "gmall_order",
            table = {"cart_info"},
            eventType = {CanalEntry.EventType.UPDATE,
                    CanalEntry.EventType.INSERT,
                    CanalEntry.EventType.DELETE
            })
    public void onEvent4(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {


        CartInfo cartInfo = new CartInfo();
        rowData.getAfterColumnsList().forEach((c) ->{
                System.err.println("Key: " + c.getName() + " :: Value::  " + c.getValue());
                /*        try {
                cartInfo.getClass().getDeclaredField(c.getName()).set(c.getName(),c.getValue());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }*/
                if ("id".equals(c.getName())){
                cartInfo.setId(Long.parseLong(c.getValue()));
            }else if ("user_id".equals(c.getName())){
                cartInfo.setUserId(c.getValue());
            }else if ("sku_id".equals(c.getName())) {
                cartInfo.setSkuId(Long.parseLong(c.getValue()));
            }else if ("cart_price".equals(c.getName())){
                cartInfo.setCartPrice(new BigDecimal(c.getValue()));
            }else if ("sku_num".equals(c.getName())){
                cartInfo.setSkuNum(Integer.parseInt(c.getValue()));
            }else if ("img_url".equals(c.getName())){
                cartInfo.setImgUrl(c.getValue());
            }else if("sku_name".equals(c.getName())){
                cartInfo.setSkuName(c.getValue());
            }else if ("is_checked".equals(c.getName())){
                cartInfo.setIsChecked(Integer.parseInt(c.getValue()));
            }
        });

           if(eventType.name().equals(CanalEntry.EventType.DELETE)){
            //应该怎么处理
        }
        System.out.println("num:" + cartInfo.getSkuNum());
           /*        redisTemplate.opsForHash().delete(RedisConst.USER_KEY_PREFIX +
                        cartInfo.getUserId() + RedisConst.USER_CART_KEY_SUFFIX,
                cartInfo.getSkuId().toString());*/
        redisTemplate.opsForHash().put(RedisConst.USER_KEY_PREFIX+
                cartInfo.getUserId() + RedisConst.USER_CART_KEY_SUFFIX,
                cartInfo.getSkuId().toString(),cartInfo);
        //更新Redis 五大数据  Value值类型 Set类型 ZSet
        // Map user:3:cart = new HashMap();
        //张三.put(skuId.toString,CartInfo)
        //张三.put(skuId.toString,CartInfo)
        //张三.put(skuId.toString,CartInfo)
        //张三.put(skuId.toString,CartInfo)
        //张三.put(skuId.toString,CartInfo)
        // 张三 ： skuId:1   V:CartInfo:
        // 张三 ： skuId: 2  V:CartInfo
        // 张三 ： 3  18
    }


}
